const fs = require('fs');
const path = require('path');
const http = require('http');

// 读取政采云销售订单数据文件
const rawData = fs.readFileSync(path.join(__dirname, '通过插件拦截响应数据（政采云销售订单）.md'), 'utf8');
const jsonData = JSON.parse(rawData);

// 准备发送的数据
const postData = JSON.stringify(jsonData);

// 配置请求选项
const options = {
  hostname: '127.0.0.1',
  port: 9001,
  path: '/api/v1/plugin/data/receive',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(postData)
  }
};

// 发送请求
const req = http.request(options, (res) => {
  console.log(`状态码: ${res.statusCode}`);
  
  res.on('data', (chunk) => {
    console.log(`响应主体: ${chunk}`);
  });
  
  res.on('end', () => {
    console.log('响应结束');
  });
});

req.on('error', (e) => {
  console.error(`请求遇到问题: ${e.message}`);
});

// 写入数据到请求主体
req.write(postData);
req.end();

console.log('数据已发送到API接口');